$(document).ready(function() {
	MC.TransactionModel = Backbone.Model.extend();
	
	MC.TransactionCollection = Backbone.Collection.extend({
		url: MC.appName + "transaction/getTransactionsJSON",
		
		model: MC.TransactionModel
	});
	
	// Initialize
	MC.transactionCollection = new MC.TransactionCollection();
	MC.transactionCollection.reset(MC.transactionsJSON);
	console.log(MC.transactionCollection);
	
	MC.TransactionRow = Backbone.View.extend({
		events: {
			"blur #detail": "saveDescription",
			"change #detail": "setDescription",
			"change #category": "setCategory"
		}, 
		
		setDescription: function(){
			console.log("setDescription");
			this.model.set({
				"description": this.$('#detail').val()
			});
		},
		
		saveDescription: function() {
			console.log("saveDescription" + this.model.get('id'));
			var params = {};
			params.id = this.model.get('id');
			params.description = this.model.get('description');
			$.post(MC.appName + "transaction/setDescription", params, function(data) {
				console.log(data);
			});
		},
		
		setCategory: function() {
			console.log('setCategory');
			var params = {};
			params.transactionId = this.model.get('id');
			params.categoryId = this.$('#category').val();
			$.post(MC.appName + "transactionCategory/setTransactionCatgory", params, function(data) {
				console.log(data);
			});			
		}
	});
	
	_.each(MC.transactionCollection.models, function(model){
		console.log(model);
		var txnRow = new MC.TransactionRow({
			el: "#transaction-row-" + model.get('id'),
			model: model
		});
	});
});